package com.xmlt.ui.launcher;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;

public class XMLTLaunchTab extends AbstractLaunchConfigurationTab {
	
	public static final String ATTR_PROJECT_NAME = "com.xmlt.ui.launcher.projectName";
	public static final String ATTR_OUTPUT_DIR = "com.xmlt.ui.launcher.outputDir";

	private Text projectText;
	private Button projectButton;
	private WidgetListener widgetListener = new WidgetListener();
	
	private Text dirText;
	private Button dirButton;

	@Override
	public void createControl(Composite parent) {
		Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayout(new GridLayout());
		createProjectEditor(comp);
		createDirEditor(comp);
		setControl(comp);
	}

	@Override
	public String getName() {
		return "Project Selection";
	}

	@Override
	public void performApply(ILaunchConfigurationWorkingCopy config) {
		config.setAttribute(ATTR_PROJECT_NAME, projectText.getText().trim());
		config.setAttribute(ATTR_OUTPUT_DIR, dirText.getText().trim());
	}

	@Override
	public void setDefaults(ILaunchConfigurationWorkingCopy config) {
		String projectName = null;
		String outputDir = null;
		try {
			projectName = config.getAttribute(ATTR_PROJECT_NAME, projectName);
			outputDir = config.getAttribute(ATTR_OUTPUT_DIR, outputDir);
		} catch (CoreException e) {
		}
		if (projectName == null || "".equals(projectName))
			return;
		projectText.setText(projectName);
		
		if (outputDir == null || "".equals(outputDir))
			return;
		dirText.setText(outputDir);
	}

	@Override
	public void initializeFrom(ILaunchConfiguration config) {
		String projectName = null;
		String outputDir = null;
		try {
			projectName = config.getAttribute(ATTR_PROJECT_NAME, projectName);
			outputDir = config.getAttribute(ATTR_OUTPUT_DIR, outputDir);
		} catch (CoreException e) {
		}
		if (projectName == null || "".equals(projectName))
			return;
		projectText.setText(projectName);
		
		if (outputDir == null || "".equals(outputDir))
			return;
		dirText.setText(outputDir);
	}

	protected void createProjectEditor(Composite parent) {
		Font font = parent.getFont();
		Group group = new Group(parent, SWT.NONE);
		group.setText("Project");
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		group.setLayoutData(gd);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		group.setLayout(layout);
		group.setFont(font);
		projectText = new Text(group, SWT.SINGLE | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		projectText.setLayoutData(gd);
		projectText.setFont(font);
		projectText.addModifyListener(widgetListener);
		projectButton = createPushButton(group, "...", null);
		projectButton.addSelectionListener(widgetListener);
	}
	
	protected void createDirEditor(Composite parent) {
		Font font = parent.getFont();
		Group group = new Group(parent, SWT.NONE);
		group.setText("Output directory");
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		group.setLayoutData(gd);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		group.setLayout(layout);
		group.setFont(font);
		dirText = new Text(group, SWT.SINGLE | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		dirText.setLayoutData(gd);
		dirText.setFont(font);
		dirText.addModifyListener(widgetListener);
		dirButton = createPushButton(group, "...", null);
		dirButton.addSelectionListener(widgetListener);
	}

	private IProject chooseProject() {
		ILabelProvider labelProvider = new XMLTProjectLabelProvider();
		ElementListSelectionDialog dialog = new ElementListSelectionDialog(
				getShell(), labelProvider);
		dialog.setTitle("XMLT Project");
		dialog.setMessage("Select a project");
		dialog.setElements(getWorkspaceRoot().getProjects());
		IProject javaProject = getProject();
		if (javaProject != null) {
			dialog.setInitialSelections(new Object[] { javaProject });
		}
		if (dialog.open() == Window.OK) {
			return (IProject) dialog.getFirstResult();
		}
		return null;
	}
	
	protected void handleDirButtonSelected() {
		DirectoryDialog dirDialog = new DirectoryDialog(getShell());
		String path = dirDialog.open();
		if (path != null && !"".equals(path));
			dirText.setText(path);
	}

	protected void handleProjectButtonSelected() {
		IProject project = chooseProject();
		if (project == null) {
			return;
		}
		String projectName = project.getName();
		projectText.setText(projectName);
	}

	private IProject getProject() {
		return null;
	}

	protected IWorkspaceRoot getWorkspaceRoot() {
		return ResourcesPlugin.getWorkspace().getRoot();
	}

	private class WidgetListener implements ModifyListener, SelectionListener {

		public void modifyText(ModifyEvent e) {
			updateLaunchConfigurationDialog();
		}

		public void widgetDefaultSelected(SelectionEvent e) {/* do nothing */
		}

		public void widgetSelected(SelectionEvent e) {
			Object source = e.getSource();
			if (source == projectButton) {
				handleProjectButtonSelected();
			} else if (source == dirButton) {
				handleDirButtonSelected();
			}
			else {
				updateLaunchConfigurationDialog();
			}
		}
		
		public void buttonSelected() {
			handleProjectButtonSelected();
		}
	}
	
}
